package objReader;

import java.util.regex.*;

public class Face 
{
	static final String patternString = "^([0-9]*)/([0-9]*)/([0-9]*)";
	static Pattern p = Pattern.compile(patternString);
	static Matcher m;
	
	public Vertex3 v_index; 
	public Vertex3 vt_index;
	public Vertex3 vn_index;
	
	public int materialIndex;
	public boolean texturedFace;
	
	static int[] tmp;
	
	public Face(String str1, String str2, String str3, int matIndex)
	{
		v_index = new Vertex3(-1, -1, -1);
		vt_index = new Vertex3(-1, -1, -1);
		vn_index = new Vertex3(-1, -1, -1);
		tmp = new int[3];
		
		faceStringParser(str1, tmp);
		v_index.x = tmp[0];
		vt_index.x = tmp[1];
		vn_index.x = tmp[2];
		
		faceStringParser(str2, tmp);
		v_index.y = tmp[0];
		vt_index.y = tmp[1];
		vn_index.y = tmp[2];
		
		faceStringParser(str3, tmp);
		v_index.z = tmp[0];
		vt_index.z = tmp[1];
		vn_index.z = tmp[2];
		
		this.materialIndex = matIndex;
	}
	
	/* Examples of str 
	 * "5/13/6" (Contains all information)
	 * "4//1"   (Does not contain texture data)
	 * "10//"	(Only contains vertex data to make up the geometry. No textures or normals found in obj file)
	 */
	private void faceStringParser(String str, int[] buf)
	{
		int v = -1;
		int vt = -1;
		int vn = -1;
		
		m = p.matcher(str);
		
		if(m.find())
		{
			if(!m.group(1).isEmpty())
				v = Integer.parseInt(m.group(1));
		
			if(!m.group(2).isEmpty())
				vt = Integer.parseInt(m.group(2));
			
			if(!m.group(3).isEmpty())
				vn = Integer.parseInt(m.group(3));
		}
		buf[0] = v;
		buf[1] = vt;
		buf[2] = vn;
	}
}
